package com.ocom.common.utils;

import java.text.DecimalFormat;

/**
 * 数据类型转换工具类
 *
 * @author cyf
 */
public class NumConvertUtil {


	/**
	 * bytes 转16进制字符串
	 *
	 * @param bArray
	 * @return
	 */
	public static final String bytesToHexString(byte[] bArray) {
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		for (int i = 0; i < bArray.length; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toLowerCase());
		}
		return sb.toString();
	}


	/**
	 * 16进制字符串转bytes
	 *
	 * @param hex
	 * @return
	 */
	public static byte[] hexStringToByte(String hex) {

		int len = 0;
		int num = 0;

		//判断字符串的长度是否是两位
		if (hex.length() >= 2) {

			//判断字符喜欢是否是偶数
			len = (hex.length() / 2);
			num = (hex.length() % 2);

			if (num == 1) {
				hex = "0" + hex;
				len = len + 1;
			}


		} else {

			hex = "0" + hex;
			len = 1;


		}


		byte[] result = new byte[len];
		char[] achar = hex.toCharArray();
		for (int i = 0; i < len; i++) {
			int pos = i * 2;
			result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		}
		return result;

	}


	private static int toByte(char c) {


		if (c >= 'a')
			return (c - 'a' + 10) & 0x0f;
		if (c >= 'A')
			return (c - 'A' + 10) & 0x0f;
		return (c - '0') & 0x0f;


	}


	/**
	 * 16进制字符串转十进制int
	 *
	 * @param HexString
	 * @return
	 */
	public static Integer HexStringToInt(String HexString) {

		int inJTFingerLockAddress = Integer.valueOf(HexString, 16);

		return inJTFingerLockAddress;
	}

	/**
	 * @param str16
	 * @return
	 * @Date:2014-3-18
	 * @Author:lulei
	 * @Description: 将16进制的字符串转化为long值
	 */
	public static Long HexStringToLong(String str16){
		if (str16 == null) {
			throw new NumberFormatException("null");
		}
		//先转化为小写
		str16 = str16.toLowerCase();
		//如果字符串以0x开头，去掉0x
		str16 = str16.startsWith("0x") ? str16.substring(2) : str16;
		if (str16.length() > 16) {
			throw new NumberFormatException("For input string '" + str16 + "' is to long");
		}
		return parseMd5L16ToLong(str16);
	}
	/**
	 * @param md5L16
	 * @return
	 * @Date:2014-3-18
	 * @Author:lulei
	 * @Description: 将16位的md5转化为long值
	 */
	public static long parseMd5L16ToLong(String md5L16){
		if (md5L16 == null) {
			throw new NumberFormatException("null");
		}
		md5L16 = md5L16.toLowerCase();
		byte[] bA = md5L16.getBytes();
		long re = 0L;
		for (int i = 0; i < bA.length; i++) {
			//加下一位的字符时，先将前面字符计算的结果左移4位
			re <<= 4;
			//0-9数组
			byte b = (byte) (bA[i] - 48);
			//A-F字母
			if (b > 9) {
				b = (byte) (b - 39);
			}
			//非16进制的字符
			if (b > 15 || b < 0) {
				throw new NumberFormatException("For input string '" + md5L16);
			}
			re += b;
		}
		return re;
	}



	/**
	 * 十进制int转16进制字符串
	 *
	 * @param num
	 * @return
	 */
	public static String IntToHexString(int num) {

		String hexString = Integer.toHexString(num);

		return hexString;
	}

	/**
	 * 十进制int转16进制字符串
	 *
	 * @param num
	 * @return
	 */
	public static String IntToHexStringNum(int num, Integer length) {

		String hexString = Integer.toHexString(num);
		if (hexString.length() < length) {
			StringBuilder stringBuilder = new StringBuilder(hexString);
			for (int i = 0; i < (length - hexString.length()); i++) {
				stringBuilder.insert(0, "0");
			}
			return stringBuilder.toString();
		}

		return hexString;
	}

	public static String LongToHexStringNum(Long num, Integer length) {
		String hexString =  Long.toHexString(num).toUpperCase();
		if (hexString.length() < length) {
			StringBuilder stringBuilder = new StringBuilder(hexString);
			for (int i = 0; i < (length - hexString.length()); i++) {
				stringBuilder.insert(0, "0");
			}
			return stringBuilder.toString();
		}
		return hexString;
	}


	/**
	 * 十进制->补0的字符串
	 */
	public static String IntLegth(Integer num, Integer length) {
		String str = num.toString();
		if (str.length() < length) {
			StringBuilder stringBuilder = new StringBuilder(str);
			for (int i = 0; i < (length - str.length()); i++) {
				stringBuilder.insert(0, "0");
			}
			return stringBuilder.toString();
		}
		return str;
	}

	/**
	 * 16进制String转BCD
	 *
	 * @param asc
	 * @return
	 */
	public static byte[] strToBcd(String asc) {
		int len = asc.length();
		int mod = len % 2;

		if (mod != 0) {
			asc = "0" + asc;
			len = asc.length();
		}

		byte abt[] = new byte[len];
		if (len >= 2) {
			len = len / 2;
		}

		byte bbt[] = new byte[len];
		abt = asc.getBytes();
		int j, k;

		for (int p = 0; p < asc.length() / 2; p++) {
			if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
				j = abt[2 * p] - '0';
			} else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
				j = abt[2 * p] - 'a' + 0x0a;
			} else {
				j = abt[2 * p] - 'A' + 0x0a;
			}

			if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
				k = abt[2 * p + 1] - '0';
			} else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
				k = abt[2 * p + 1] - 'a' + 0x0a;
			} else {
				k = abt[2 * p + 1] - 'A' + 0x0a;
			}

			int a = (j << 4) + k;
			byte b = (byte) a;
			bbt[p] = b;
		}
		return bbt;
	}


	/**
	 * String 类型数字转化为Double  保留置顶小数位(用于显示金额等。)
	 *
	 * @param money
	 * @param type  保留小数点位数  #.00保留两位  #.0保留一位  #保留整数
	 * @return
	 */
	public static String strToDouble(String money, String type) {

		String toDouble = new DecimalFormat(type).format(Double.parseDouble(money));

		return toDouble;

	}


	/**
	 * int到byte[] 由高位到低位
	 *
	 * @param i 需要转换为byte数组的整行值。
	 * @return byte数组
	 */
	public static byte[] intToByteArray(int i) {
		byte[] result = new byte[4];
		result[0] = (byte) ((i >> 24) & 0xFF);
		result[1] = (byte) ((i >> 16) & 0xFF);
		result[2] = (byte) ((i >> 8) & 0xFF);
		result[3] = (byte) (i & 0xFF);
		return result;
	}


	public static String byteToHex(Integer... params) {
		StringBuilder builder = new StringBuilder();
		for (Integer s : params) {
			int v = s & 0xFF;
			String colorHex = Integer.toHexString(v);
			if (1 == colorHex.length()) {
				builder.append('0');
			}
			builder.append(colorHex);
		}
		return builder.toString();
	}

	public static String byteToHex(byte... params) {
		StringBuilder builder = new StringBuilder();
		for (Byte s : params) {
			int v = s & 0xFF;
			String colorHex = Integer.toHexString(v);
			if (1 == colorHex.length()) {
				builder.append('0');
			}
			builder.append(colorHex);
		}
		return builder.toString();
	}


	public static String hexYihuo(String content) {
		content = change(content);
		String[] b = content.split(" ");
		int a = 0;
		for (int i = 0; i < b.length; i++) {
			a = a ^ Integer.parseInt(b[i], 16);
		}
		if (a < 10) {
			StringBuffer sb = new StringBuffer();
			sb.append("0");
			sb.append(a);
			return sb.toString();
		}
		return Integer.toHexString(a);
	}

	public static String change(String content) {
		String str = "";
		for (int i = 0; i < content.length(); i++) {
			if (i % 2 == 0) {
				str += " " + content.substring(i, i + 1);
			} else {
				str += content.substring(i, i + 1);
			}
		}
		return str.trim();
	}

	public static String hexLeijia(String data) {
		if (data == null || data.equals("")) {
			return "";
		}
		int total = 0;
		int len = data.length();
		int num = 0;
		while (num < len) {
			String s = data.substring(num, num + 2);
//            System.out.println(s);
			total += Integer.parseInt(s, 16);
			num = num + 2;
		}/*** 用256求余最大是255，即16进制的FF */
		int mod = total % 256;
		String hex = Integer.toHexString(mod);
		len = hex.length(); // 如果不够校验位的长度，补0,这里用的是两位校验
		if (len < 2) {
			hex = "0" + hex;
		}
		return hex;
	}

	public static String reverseHex(final String hex) {
		final char[] charArray = hex.toCharArray();
		final int length = charArray.length;
		final int times = length / 2;
		for (int c1i = 0; c1i < times; c1i += 2) {
			final int c2i = c1i + 1;
			final char c1 = charArray[c1i];
			final char c2 = charArray[c2i];
			final int c3i = length - c1i - 2;
			final int c4i = length - c1i - 1;
			charArray[c1i] = charArray[c3i];
			charArray[c2i] = charArray[c4i];
			charArray[c3i] = c1;
			charArray[c4i] = c2;
		}
		return new String(charArray);
	}


	public static String hexString2binaryString(String hexString) {
		if (hexString == null || hexString.length() % 2 != 0)
			return null;
		String bString = "", tmp;
		for (int i = 0; i < hexString.length(); i++) {
			tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
			bString += tmp.substring(tmp.length() - 4);
		}
		return bString;
	}

	public static String binaryString2hexString(String bString) {
		if (bString == null || bString.equals("") || bString.length() % 8 != 0)
			return null;
		StringBuffer tmp = new StringBuffer();
		int iTmp = 0;
		for (int i = 0; i < bString.length(); i += 4) {
			iTmp = 0;
			for (int j = 0; j < 4; j++) {
				iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
			}
			tmp.append(Integer.toHexString(iTmp));
		}
		return tmp.toString();
	}


	/**
	 * 16进制转换成为string类型字符串
	 *
	 * @param s
	 * @return
	 */
	public static String hexStringToString(String s) {
		if (s == null || s.equals("")) {
			return null;
		}
		s = s.replace(" ", "");
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			s = new String(baKeyword, "UTF-8");
			new String();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return s;
	}

	/**
	 * 字符串转换为16进制字符串
	 *
	 * @param s
	 * @return
	 */
	public static String stringToHexString(String s) {
		String str = "";
		for (int i = 0; i < s.length(); i++) {
			int ch = s.charAt(i);
			String s4 = Integer.toHexString(ch);
			str = str + s4;
		}
		return str;
	}

	public static String toGBK(String source){
		try {
			StringBuilder sb = new StringBuilder();
			byte[] bytes = source.getBytes("GBK");
			for (byte b : bytes) {
				sb.append(Integer.toHexString((b & 0xff)).toUpperCase());
			}
			return sb.toString();
		}catch (Exception e){

		}
		return  "CCE1CABEBDE2CEF6D2ECB3A3";

	}

	// 16进制直接转换成为汉字
	public static String hexStr2Str(String hexStr) {
		String str = "0123456789ABCDEF";
		char[] hexs = hexStr.toCharArray();
		byte[] bytes = new byte[hexStr.length() / 2]; //1个byte数值 -> 两个16进制字符
		int n;
		for (int i = 0; i < bytes.length; i++) {
			n = str.indexOf(hexs[2 * i]) * 16;
			n += str.indexOf(hexs[2 * i + 1]);
			bytes[i] = (byte) (n & 0xff);
		}
		return new String(bytes);
	}


	// 转化十六进制编码为字符串
	public static String toStringHex2(String s) {
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(
						i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			s = new String(baKeyword, "utf-8");// UTF-16le:Not
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return s;
	}




}
